package com.ruoyi.base.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @BelongsProject: RuoYi-Cloud-master
 * @BelongsPackage: com.ruoyi.base.config
 * @Author: songYuHang
 * @CreateTime: 2025-02-24  09:59
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入时，自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        this.strictInsertFill(metaObject, "createBy", String.class, loginUser.getUsername());
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
    }

    /**
     * 修改时自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        this.strictUpdateFill(metaObject, "updateBy", String.class, loginUser.getUsername());
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
    }
}
